package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.SystemInforModel;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.view.HorizontalDelete;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by wangpan on 2018/6/11.
 */

public class InformationAdapter extends BaseAdapter {

    private Context context;
    private HorizontalDelete hd;
    private OnItemClickListener listener;
    private List<SystemInforModel.DataBean> list;
    private SharedPreferences sp;

    public InformationAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        sp = this.context.getSharedPreferences("isRead", MODE_PRIVATE);
    }

    public void setUpData() {

    }

    public void setDownData(List<SystemInforModel.DataBean> beanList) {
        this.list = beanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public SystemInforModel.DataBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.information_item_layout, null);
            viewHolder = new ViewHolder();
            viewHolder.title = convertView.findViewById(R.id.title);
            viewHolder.content = convertView.findViewById(R.id.content);
            viewHolder.date = convertView.findViewById(R.id.data);
            //   viewHolder.delete_button = convertView.findViewById(R.id.delete_button);
            //    viewHolder.hd = convertView.findViewById(R.id.delete);
            convertView.setTag(R.id.tag_one, viewHolder);
            convertView.setTag(R.id.tag_two, position);
        } else {
            viewHolder = (ViewHolder) convertView.getTag(R.id.tag_one);
        }
        if(sp.getBoolean(String.valueOf(list.get(position).getId()),false)){
            viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_default));
            viewHolder.content.setTextColor(context.getResources().getColor(R.color.text_dan));
            viewHolder.date.setTextColor(context.getResources().getColor(R.color.text_dan));
        }else{
            viewHolder.title.setTextColor(context.getResources().getColor(R.color.text_black));
            viewHolder.content.setTextColor(context.getResources().getColor(R.color.text_default));
            viewHolder.date.setTextColor(context.getResources().getColor(R.color.text_default));
        }
        viewHolder.title.setText("系统通知");
        viewHolder.content.setText(list.get(position).getTitle());
        viewHolder.date.setText(TimeUtil.format2(list.get(position).getPublishTime()));
//        viewHolder.hd.setCustomOnClickListener(new HorizontalDelete.CustomOnClickListener() {
//            @Override
//            public void onClick() {
//                listener.CallBack(position);
//            }
//        });
//        viewHolder.hd.setListener(new HorizontalDelete.HorizontalDeleteMoveListener() {
//            @Override
//            public void open(HorizontalDelete horizontalDelete) {
//                holdSlidingMenu(horizontalDelete);
//            }
//
//            @Override
//            public void close() {
//                closeSlidingMenu();
//            }
//        });
//        viewHolder.delete_button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //删除
//            //    delete(item.getUuid());
//                XToast.showToast(context,"删除"+position);
//            }
//        });
        return convertView;
    }

    private class ViewHolder {
        TextView title, content, date;
        RelativeLayout delete_button;
        HorizontalDelete hd;
    }

    //设置为当前
    public void holdSlidingMenu(HorizontalDelete hd) {
        this.hd = hd;
    }

    //关闭
    public void closeSlidingMenu() {
        if (hd != null && hd.isOpen()) {
            hd.closeMenu();
        }
    }

    public interface OnItemClickListener {
        void CallBack(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
